package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.EhyModule;
import com.cqut.hfh.ordering.pojo.EhyRole;
import com.cqut.hfh.ordering.pojo.EhyRoleModule;
import com.cqut.hfh.ordering.service.IModuleService;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LJH
 * @date 2019/6/22-22:16
 */
@Service
public class ModuleServiceImpl implements IModuleService {

    @Resource
    private SearchDao searchDao;
    @Resource
    private Dao dao;


    @Override
    public List<EhyModule> getModulesById(String role_id) {

        String condition = " ehy_role_module.role_id = '" + role_id + "'";
        String joinEntity = "left join ehy_module on ehy_role_module.mod_id = ehy_module.mod_id";
        List<Map<String, Object>> temp_list = searchDao.searchForeign(new String[]{"ehy_module.url",
                "ehy_module.mod_name",
                "ehy_module.mod_code",
                "ehy_module.mod_id"},
                Entity.getTableName(EhyRoleModule.class),
                joinEntity,
                null,
                null,
                condition);
        if(temp_list == null){
            return null;
        }

        List<EhyModule> ehyModule_list = new ArrayList<>();
        for( Map<String, Object> temp : temp_list){
            EhyModule ehyModule_temp = new EhyModule();
            ehyModule_temp.setProperties(temp);
            ehyModule_list.add(ehyModule_temp);
        }

        return ehyModule_list;
    }

    @Override
    public List<EhyModule> getModules() {
        return dao.getByCondition(null,EhyModule.class);
    }

    @Override
    public List<EhyRoleModule> getRoleModule() {
        return dao.getByCondition(" 1=1", EhyRoleModule.class);
    }

    @Override
    public int deleteById(String mod_id) {
        dao.deleteByCondition(" mod_id = '" + mod_id + "'", EhyRoleModule.class);
        return dao.deleteByID(mod_id, EhyModule.class);
    }


    @Override
    public List<Map<String, Object>> getModules(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> mods_list
                = dao.findByCondition(new String[]{"mod_id",
                        "ehy_mod_id",
                        "url",
                        "mod_name",
                        "mod_code",
                        "mod_desc",
                        "isva",
                        "optime",
                        "oper",
                        "sort"},
                con,
                EhyModule.class);

        return mods_list;
    }

    @Override
    public Integer getCount() {
        return searchDao.sqlCount(Entity.getTableName(EhyModule.class));
    }

    @Override
    public EhyModule findById(String mod_id) {
        EhyModule e = dao.getByID(mod_id, EhyModule.class);
        return e;
    }

    @Override
    public List<Map<String, Object>> getModulesByName(String limit, String page, String role_name) {
        String con = " mod_name like '%" + role_name + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        List<Map<String, Object>> mods_list
                = dao.findByCondition(new String[]{"mod_id",
                        "ehy_mod_id",
                        "url",
                        "mod_name",
                        "mod_code",
                        "mod_desc",
                        "oper",
                        "optime",
                        "sort",
                        "isva"},
                con,
                EhyModule.class);

        return mods_list;
    }

    @Override
    public int saveModule(EhyModule module) {
        dao.save(module);
        return 1;
    }

    @Override
    public int updateModule(EhyModule module) {
        dao.updatePropByID(module, module.getMod_id());
        return 1;
    }
}
